package com.tebiecloud.mpopen.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tebiecloud.mpopen.client.model.entity.MpopenAuthorizationInfo;
import com.tebiecloud.mpopen.server.service.MpopenAuthorizationInfoService;
import com.tebiecloud.common.model.PageParams;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.Map;

/**
 * 公众号接口调用凭据和授权信息 前端控制器
 * @author tebie
 * @date 2019-09-19
 */
@RestController
@RequestMapping("/mpopenauthorizationinfo")
public class MpopenAuthorizationInfoController {

    @Autowired
    private MpopenAuthorizationInfoService mpopenAuthorizationInfoService;

    /**
     * 获取分页数据
     * @param map
     * @return
     */
    @GetMapping(value = "list")
    public ResultBody<IPage<MpopenAuthorizationInfo>> list(@RequestParam(required = false) Map map) {
        PageParams pageParams = new PageParams(map);
        MpopenAuthorizationInfo query = pageParams.mapToObject(MpopenAuthorizationInfo.class);
        QueryWrapper<MpopenAuthorizationInfo> queryWrapper = new QueryWrapper();
        return ResultBodyUtils.ok(mpopenAuthorizationInfoService.page(new PageParams(map), queryWrapper));
    }

    /**
     * 根据ID查找数据
     * @param id
     * @return
     */
    @GetMapping("get")
    public ResultBody<MpopenAuthorizationInfo> get(@RequestParam("id") Long id) {
        MpopenAuthorizationInfo entity = mpopenAuthorizationInfoService.getById(id);
        return ResultBodyUtils.ok(entity);
    }

    /**
     * 添加数据
     * @param authorizerAppid 授权方appid,
     * @param authorizerAccessToken 授权方接口调用凭据（在授权的公众号或小程序具备API权限时，才有此返回值），也简称为令牌,
     * @param expiresIn 有效期（在授权的公众号或小程序具备API权限时，才有此返回值）,
     * @param authorizerRefreshToken 接口调用凭据刷新令牌（在授权的公众号具备API权限时，才有此返回值）,
     * @param funcInfo 授权给开发者的权限集列表
     * @return
     */
    @PostMapping("add")
    public ResultBody add(
        @RequestParam(value = "authorizerAppid", required = false) String authorizerAppid,
        @RequestParam(value = "authorizerAccessToken", required = false) String authorizerAccessToken,
        @RequestParam(value = "expiresIn", required = false) Integer expiresIn,
        @RequestParam(value = "authorizerRefreshToken", required = false) String authorizerRefreshToken,
        @RequestParam(value = "funcInfo", required = false) String funcInfo
    ) {
        MpopenAuthorizationInfo entity = new MpopenAuthorizationInfo();
        entity.setAuthorizerAppid(authorizerAppid);
        entity.setAuthorizerAccessToken(authorizerAccessToken);
        entity.setExpiresIn(expiresIn);
        entity.setAuthorizerRefreshToken(authorizerRefreshToken);
        entity.setFuncInfo(funcInfo);
        mpopenAuthorizationInfoService.save(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 更新数据
     * @param authorizerAppid 授权方appid
     * @param authorizerAccessToken 授权方接口调用凭据（在授权的公众号或小程序具备API权限时，才有此返回值），也简称为令牌
     * @param expiresIn 有效期（在授权的公众号或小程序具备API权限时，才有此返回值）
     * @param authorizerRefreshToken 接口调用凭据刷新令牌（在授权的公众号具备API权限时，才有此返回值）
     * @param funcInfo 授权给开发者的权限集列表
     * @return
     */
    @PostMapping("update")
    public ResultBody update(
            @RequestParam(value = "authorizationInfoId", required = false) Long authorizationInfoId,
            @RequestParam(value = "authorizerAppid", required = false) String authorizerAppid,
            @RequestParam(value = "authorizerAccessToken", required = false) String authorizerAccessToken,
            @RequestParam(value = "expiresIn", required = false) Integer expiresIn,
            @RequestParam(value = "authorizerRefreshToken", required = false) String authorizerRefreshToken,
            @RequestParam(value = "funcInfo", required = false) String funcInfo
    ) {
        MpopenAuthorizationInfo entity = new MpopenAuthorizationInfo();
        entity.setAuthorizationInfoId(authorizationInfoId);
        entity.setAuthorizerAppid(authorizerAppid);
        entity.setAuthorizerAccessToken(authorizerAccessToken);
        entity.setExpiresIn(expiresIn);
        entity.setAuthorizerRefreshToken(authorizerRefreshToken);
        entity.setFuncInfo(funcInfo);
        mpopenAuthorizationInfoService.updateById(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 删除数据
     * @param id 单个id
     * @param ids 多个id用,号隔开
     * @return
     */
    @PostMapping("remove")
    public ResultBody remove(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "ids", required = false) String ids
    ) {
        if(id != null && id > 0) {
            mpopenAuthorizationInfoService.removeById(id);
        }
        if(ids != null && ids.length() > 0) {
            mpopenAuthorizationInfoService.removeByIds(Arrays.asList(ids.split(",")));
        }
        return ResultBodyUtils.ok();
    }

}
